package com.ywq.app.web.mvc.index;


import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ywq.app.biz.ArtActivityBiz;
import com.ywq.app.biz.AuxContentBiz;
import com.ywq.app.domain.ArtActivity;
import com.ywq.app.domain.ArtActivityMember;
import com.ywq.app.domain.Channel;
import com.ywq.app.domain.Content;
import com.ywq.app.domain.OnLineUserListener;
import com.ywq.app.web.mvc.artRoom.ArtRoomController;
import com.ywq.common.exception.AppBusinessException;
import com.ywq.common.page.PageModel;

@Controller
public class IndexController {
	private final static Logger logger = LoggerFactory.getLogger(IndexController.class);
	@Autowired
	private AuxContentBiz auxContentBiz;
	@Autowired
	private ArtActivityBiz artActivityBiz;
	//进入首页
	@RequestMapping(value = "/welcome.htm")
	public String welcome(HttpServletRequest request, HttpServletResponse response) {
		//取得首页内容
		try {
			//浮动图fudongtu
			List<Content> fudongtu = auxContentBiz.getRoomByChannel("fudongtu", 4);
			//主图
			List<Content> zhutu = auxContentBiz.getRoomByChannel("zhutu", 4);
			//公告gonggao
			List<Content> gonggao = auxContentBiz.getRoomByChannel("gonggao", 3);
			request.setAttribute("fudongtu", fudongtu);
			request.setAttribute("zhutu", zhutu);
			if(zhutu.size()>0){
				request.setAttribute("zhutuSize", zhutu.size());
			}
			request.setAttribute("gonggao", gonggao);
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/index/index";
	}
	//活动首页
	@RequestMapping(value = "/activity.htm")
	public String activity(HttpServletRequest request, HttpServletResponse response) {
		try {
			//
			List<Content> lunbotu = auxContentBiz.getRoomByChannel("activityTop", 6);
			request.setAttribute("lunbotu", lunbotu);
			List<Content> rightUp = auxContentBiz.getRoomByChannel("activityrightUp", 2);
			request.setAttribute("rightUp", rightUp);
			//近期活动    8个
			List<ArtActivity> nowRList =artActivityBiz.getNowActivityList();
			if(nowRList.size()>0){
				request.setAttribute("nowR", nowRList);
				request.setAttribute("nowCount", nowRList.size());
			}
			
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/activity/activity";
	}
	//活动详细页面
	@RequestMapping(value = "/showact.htm")
	public String showActivityById(String id,HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Content> activityRemark = auxContentBiz.getRoomByChannel("activityRemark", 1);
			if(activityRemark.size()>0){
				request.setAttribute("activityRemark", activityRemark.get(0).getDescription());
			}
			ArtActivity artAct =artActivityBiz.getArtActivityById(id);
			request.setAttribute("artAct", artAct);
			
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/activity/showActivity";
	}
	//报名页面
	@RequestMapping(value = "/signup.htm")
	public String signUp(String id,HttpServletRequest request, HttpServletResponse response) {
			request.setAttribute("activityId", id);
		return "artOne/activity/signUp";
	}
	//报名
	
	@RequestMapping(value = "/signupmember.htm", method = RequestMethod.POST)
	public String signupmember(HttpServletRequest request,HttpServletResponse response, ArtActivityMember artActivityMember) throws IOException {
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter pw = null;
		try {
			OnLineUserListener onLineUserListener = (OnLineUserListener) request.getSession().getAttribute("onlineUser");
			if(onLineUserListener!=null){
				artActivityMember.setMemberId(onLineUserListener.getMember().getId());
				artActivityBiz.signupmember(artActivityMember);
				pw = response.getWriter();
				pw.print("success");
			}else{
				pw = response.getWriter();
				pw.print("error");
			}
		} catch (Exception e) {
			pw = response.getWriter();
			pw.print("error");
			/*if (logger.isErrorEnabled()) {
				logger.error("ArtActivity failure.", e);
			}*/
		}
		return null;
	}
	//或多更多
	@RequestMapping(value = "/activtylb.htm", method = RequestMethod.GET)
	public String activtylb(Integer page,String type,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			if (page == null) {
				page = 1;
			}
			if (type == null) {
				type = "4";
			}
			
			if ("4".equals(type)) {
				PageModel activtylb = artActivityBiz.getArtActivityList(page,null);
				request.setAttribute("activtylb", activtylb);
			}else{
				PageModel activtylb = artActivityBiz.getArtActivityList(page,type);
				request.setAttribute("activtylb", activtylb);
			}
			request.setAttribute("type", type);
			request.setAttribute("link", "/activtylb.htm");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return "artOne/activity/activtylb";
		
	}
	//日历查询
	@RequestMapping(value = "/activtyse.htm", method = RequestMethod.GET)
	public String activtyse(Integer page,String st,String ed,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			if (page == null) {
				page = 1;
			}
			PageModel activtylb = artActivityBiz.getArtActivityListOfDate(page,st,ed);
			request.setAttribute("activtylb", activtylb);
			request.setAttribute("st", st);
			request.setAttribute("ed", ed);
			request.setAttribute("link", "/activtyse.htm");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return "artOne/activity/activtylb";
		
	}
	//周
	@RequestMapping(value = "/activtyWeek.htm", method = RequestMethod.GET)
	public String activtyWeek(Integer page,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			if (page == null) {
				page = 1;
			}
			PageModel activtylb = artActivityBiz.getArtAactivtyWeek(page);
			request.setAttribute("activtylb", activtylb);
			request.setAttribute("link", "/activtyWeek.htm");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return "artOne/activity/activtylb";
		
	}
	//月
	@RequestMapping(value = "/activtyMonth.htm", method = RequestMethod.GET)
	public String activtyMonth(Integer page,HttpServletRequest request, HttpServletResponse response) throws IOException {
		try {
			if (page == null) {
				page = 1;
			}
			PageModel activtylb = artActivityBiz.getArtAactivtyMonth(page);
			request.setAttribute("activtylb", activtylb);
			request.setAttribute("link", "/activtyMonth.htm");
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("getArtPicList failure.", e);
			}
		}
		return "artOne/activity/activtylb";
		
	}
	//
	@RequestMapping(value = "/getFooter.htm")
	public String getFooter( HttpServletRequest request, HttpServletResponse response) {
		try {
			//根据mark查询频道
			Channel channelFive =auxContentBiz.getChannelByMark("downFive");
			Channel channelFour =auxContentBiz.getChannelByMark("downFour");
			Channel channelThird =auxContentBiz.getChannelByMark("downThird");
			Channel channelTwo =auxContentBiz.getChannelByMark("downTwo");
			Channel channelOne =auxContentBiz.getChannelByMark("downOne");
			//
			Channel channelFreeTell =auxContentBiz.getChannelByMark("freeTell");
			
			
			List<Content> downFive = auxContentBiz.getRoomByChannel("downFive", 6);
			List<Content> downFour = auxContentBiz.getRoomByChannel("downFour", 6);
			List<Content> downThird = auxContentBiz.getRoomByChannel("downThird", 6);
			List<Content> downTwo = auxContentBiz.getRoomByChannel("downTwo", 6);
			List<Content> downOne = auxContentBiz.getRoomByChannel("downOne", 6);
			request.setAttribute("downOne", downOne);
			request.setAttribute("downTwo", downTwo);
			request.setAttribute("downThird", downThird);
			request.setAttribute("downFour", downFour);
			request.setAttribute("downFive", downFive);
			request.setAttribute("channelFive", channelFive);
			request.setAttribute("channelFour", channelFour);
			request.setAttribute("channelThird", channelThird);
			request.setAttribute("channelTwo", channelTwo);
			request.setAttribute("channelOne", channelOne);
			request.setAttribute("channelFreeTell", channelFreeTell);
			
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/common/footerCommon";
	}
	//
	@RequestMapping(value = "/bigPic.htm")
	public String artpic( HttpServletRequest request, HttpServletResponse response) {
		
		return "artOne/common/showPic";
	}
	@RequestMapping(value = "/ourMission.htm")
	public String ourMission(HttpServletRequest request, HttpServletResponse response) {
		try {
			//
			List<Content> aboutUs = auxContentBiz.getRoomByChannel("ourMission", 1);
			request.setAttribute("aboutUs", aboutUs.get(0));
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/index/aboutUs";
	}
	@RequestMapping(value = "/mediaCenter.htm")
	public String mediaCenter(HttpServletRequest request, HttpServletResponse response) {
		try {
			//
			List<Content> aboutUs = auxContentBiz.getRoomByChannel("mediaCenter", 1);
			request.setAttribute("aboutUs", aboutUs.get(0));
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/index/aboutUs";
	}

	@RequestMapping(value = "/blog.htm")
	public String blog(HttpServletRequest request, HttpServletResponse response) {
		try {
			//
			List<Content> aboutUs = auxContentBiz.getRoomByChannel("blog", 1);
			request.setAttribute("aboutUs", aboutUs.get(0));
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/index/aboutUs";
	}
	@RequestMapping(value = "/recruitment.htm")
	public String recruitment(HttpServletRequest request, HttpServletResponse response) {
		try {
			//
			List<Content> aboutUs = auxContentBiz.getRoomByChannel("recruitment", 1);
			request.setAttribute("aboutUs", aboutUs.get(0));
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/index/aboutUs";
	}
	@RequestMapping(value = "/contactUs.htm")
	public String contactUs(HttpServletRequest request, HttpServletResponse response) {
		try {
			//
			List<Content> aboutUs = auxContentBiz.getRoomByChannel("contactUs", 1);
			request.setAttribute("aboutUs", aboutUs.get(0));
		} catch (AppBusinessException e) {
			e.printStackTrace();
		}
		return "artOne/index/aboutUs";
	}
}
